package com.atomdayl.domain.therecipe.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.atomdayl.domain.therecipe.R;
import com.atomdayl.domain.therecipe.network.RequestBuildManager;
import com.atomdayl.domain.therecipe.network.ThreadCallback;
import com.atomdayl.domain.therecipe.network.ThreadManager;

/**
 * Created by David on 2016/6/5.
 */
public class ChangePasswordActivity extends BaseActivity implements ThreadCallback{

    private EditText mUsernameTv;
    private EditText mPasswordTv;
    private EditText mConfirmPassTv;
    private Button mRegisterBtn;
    private int taskId = 1 << 7;
    private boolean isChange;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_change_password);
        initViews();
        initListener();
    }

    private void initViews() {
        mUsernameTv = (EditText) findViewById(R.id.et_username);
        mPasswordTv = (EditText) findViewById(R.id.password);
        mConfirmPassTv = (EditText) findViewById(R.id.confirm_password);
        mRegisterBtn = (Button) findViewById(R.id.register_button);
    }

    private void initListener() {
        mRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isChange) return ;
                String username = mUsernameTv.getText().toString().trim();
                String password = mPasswordTv.getText().toString().trim();
                String confirmPassword = mConfirmPassTv.getText().toString().trim();
                changePassword(username, password, confirmPassword);
            }
        });

    }

    private void changePassword(String username, String password, String confirmPass) {
        if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(confirmPass)) {
            Toast.makeText(this, "请输入完整信息", Toast.LENGTH_SHORT).show();
        }

        if(verifyPassword(username, password, confirmPass)) {
            String changePasswordUrl = RequestBuildManager.getInstance().buildChangePasswordRequest(username, password);
            ThreadManager.getInstance().exeGetTask(this,this, taskId, changePasswordUrl, false, true);
            isChange = true;
        }
    }

    private boolean verifyPassword(String username, String password, String confirmPass) {

        if(!username.contains("@")){
            Toast.makeText(this, "用户名格式不正确，请输入邮箱格式", Toast.LENGTH_SHORT).show();
            return false;
        }

        if(password.equals(confirmPass)) {
            Toast.makeText(this, "密码和新密码不能保持一致", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    public void onCallbackSucceedFromThread(String resultData, int taskId) {
        Toast.makeText(this, resultData, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCallbackFailureFromThread(String resultDate, int taskId) {
        Toast.makeText(this, "修改密码接口连接失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancelFromThread(String msg, int taskId) {

    }

}
